﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyNganHangDe.GUI
{
    static class Utils
    {
        public static void ResetPosition(this BindingSource bs, int idx)
        {
            if (idx > bs.Count - 1)
                idx = bs.Count - 1;
            if (bs.Count > 0)
                bs.Position = idx;
        }

        public static void SelectAt(this DataGridView grid, int idxRow)
        {
            if (0 <= idxRow && idxRow <= grid.Rows.Count - 1)
            {
                grid.Rows[idxRow].Selected = true;
                grid.Focus();
                grid.FindForm().ActiveControl = grid;
            }
        }

        public static void hideAndShowForm(Form parentForm, Form childForm)
        {
            childForm.Icon = parentForm.Icon;
            childForm.ShowInTaskbar = parentForm.ShowInTaskbar;
            if (parentForm.WindowState == FormWindowState.Maximized)
            {
                childForm.WindowState = FormWindowState.Maximized;
                childForm.StartPosition = FormStartPosition.CenterParent;
            }
            else
            {
                childForm.WindowState = FormWindowState.Normal;
                childForm.Location = parentForm.Location;
                childForm.Size = parentForm.Size;
                childForm.StartPosition = FormStartPosition.Manual;
            }
            childForm.FormClosed += (oSender, oE) => { parentForm.Show(); };
            childForm.Show();
            parentForm.Hide();
        }

        private static NotifyIcon _ni = null;
        public static void Notify(Form form, string title, string message, ToolTipIcon icon = ToolTipIcon.Info)
        {
            if (_ni == null)
                _ni = new NotifyIcon();
            if (form != null)
                _ni.Icon = form.Icon;
            _ni.ShowBalloonTip(3000, title, message, icon);
        }
    }
}
